import { BasicModel, BasicQuery, Page } from '../model/baseModel';
import { defHttp } from '/@/utils/http/axios';
import { EntityType } from '/@/enums/entityTypeEnum';

/**
 * 实体ID接口
 */
export interface EntityId {
  id: string;
  entityType: string;
}

/**
 * 租户分组中的租户信息（完整对象）
 */
export interface TenantInGroup {
  id: EntityId;
  createdTime: number;
  country?: string;
  state?: string;
  city?: string;
  address?: string;
  address2?: string;
  zip?: string;
  phone?: string;
  email?: string;
  title: string; // 租户名称（主要显示字段）
  name: string; // 租户名称（备用字段）
  region?: string;
  tenantProfileId?: EntityId;
  version?: number;
  additionalInfo?: Record<string, any>;
}

export interface TenantGroup extends BasicModel<EntityType.TENANT_GROUP> {
  name: string;
  description?: string;
  tenantCount?: number | null; // 可能为 null
  tenants?: TenantInGroup[]; // 完整的租户对象数组
  tenantId?: EntityId; // 所属租户ID（系统字段）
}

export function tenantGroupSave(data?: TenantGroup | any) {
  return defHttp.postJson<TenantGroup>({
    url: '/api/tenantGroup',
    data,
  });
}

export function getTenantGroupById(tenantGroupId: string) {
  return defHttp.get<TenantGroup>({
    url: `/api/tenantGroup/${tenantGroupId}`,
  });
}

export function tenantGroupDelete(tenantGroupId: string) {
  return defHttp.delete<void>({
    url: `/api/tenantGroup/${tenantGroupId}`,
  });
}

export function tenantGroupList(params: BasicQuery) {
  return defHttp.get<Page<TenantGroup>>({
    url: `/api/tenantGroups`,
    params,
  });
}

/**
 * 批量添加租户到租户分组
 * @param tenantGroupId 租户分组ID
 * @param tenantIds 租户ID列表
 */
export function tenantGroupAddTenant(tenantGroupId: string, tenantIds: string[]) {
  return defHttp.postJson<void>({
    url: '/api/tenantGroup/addTenant',
    data: {
      tenantGroupId,
      tenantIds,
    },
  });
}

/**
 * 批量从租户分组中删除租户
 * @param tenantGroupId 租户分组ID
 * @param tenantIds 租户ID列表
 */
export function tenantGroupDelTenant(tenantGroupId: string, tenantIds: string[]) {
  return defHttp.postJson<void>({
    url: '/api/tenantGroup/delTenant',
    data: {
      tenantGroupId,
      tenantIds,
    },
  });
}
